package com.springboot.middleware.middleware_redisson.server.redisson;

import com.springboot.middleware.middleware_redisson.server.dto.UserLoginDto;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserLoginPublisher {
    private static final Logger log = LoggerFactory.getLogger(UserLoginPublisher.class);
    private static final String key = "redissonUserLoginTopicKey";

    @Autowired
    RedissonClient redissonClient;

    /**
     * 异步发送消息
     * @param dto (userName, password, userId)
     */
    public void sendMsg(UserLoginDto dto) {
        try {
            if (dto != null) {
                log.info("记录用户登陆成功后的轨迹-生产者-发送消息{}", dto);
                //创建主题
                RTopic topic = redissonClient.getTopic(key);
                //发布消息
                topic.publishAsync(dto);
            }
        } catch (Exception e) {
            log.error("记录用户登陆成功后的轨迹-生产者-发生异常{}", e.fillInStackTrace());
        }
    }
}
